
.pragma library
var xmlData;
var xmlDataComment;
var xmlDataLogin;
var xmlDataUser;

var pagesXML;
var user;
var pass;
var loginOk = false;            //Si login ok, active button_user et autre option
var nb = null;
var pagesNb = 1;
var categorie = 0;
var lien;

//function setLinkVDM(link){
//    link = link_vdm;
//}
//function setStringVDM(vdm){
//    vdm = string_vdm;
//}
//function getLinkVDM(){
//    return link_vdm;
//}
//function getStringVDM(){
//    return string_vdm;
//}
function getPages(){
    console.debug("pages = " + pagesNb)
    if(pagesNb <= 1){
        pagesNb = 1;
//        console.debug("If " + pagesNb)
    }
    else if(pagesNb == nb)
    {
//        console.debug("else " + pagesNb)
        return pagesNb;
    }
    return pagesNb;
}

function pagesNext(){
    var pages = getPages();
//    console.debug("pages = " + pages)
    pages++;
    pagesNb = pages
}
function pagesPrevious(){
    var pages = getPages();
//    console.debug("pages = " + pages)
    if(getPages() == 1){
        pagesNb = 1
    }
    else{
        pages--;
        pagesNb = pages
    }
}

function setCategorie(cat){
    categorie = cat;
    console.debug("Categorie set = " + cat)
}
function getCategorie(){
    return categorie;
}
function getCategorieName(){        //Pour le titre de la page
    switch(categorie){
    case 1:                         //Amour
        return "Amour";
    case 2:                         //Animaux
        return "Animaux";
    case 3:                         //Argent
        return "Argent";
    case 4:                         //Enfant
        return "Enfant";
    case 5:                         //Travail
        return "Travail";
    case 6:                         //Sante
        return "Sante";
    case 7:                          //Sexe
        return "Sexe";
    case 8:                         //Inclassable
        return "Inclassable";
    case 9:                         //Nouv
        return "Nouveaux"
    case 10:
        return "Flop"               //Flop
    case 11:
        return "Top"                //Top
    }
}

function setLogin(usr,pss)
{
    user = usr;
    pass = pss;
}

function loginRequest(user, pass)
{
    console.debug("Login : " + user + " " + pass)
    var doc = new XMLHttpRequest();
    var link = "http://api.fmylife.com/account/login/"+user+"/"+pass+"?key=4fd9d0cb0463c&language=fr"
    doc.onreadystatechange = function()
            {
                if (doc.readyState == XMLHttpRequest.DONE)
                {
                    xmlDataLogin = doc.responseText
                }
            }
    doc.open("POST", link);
    doc.send();
}

function load_pages(obj,value)      //0 = norml; 1 = user; 2 = commenataire
{
    var doc = new XMLHttpRequest();
    var link;
    if(value === 0){        //Normal
        switch(getCategorie()){
        case 1:     //Amour
            link = "http://api.fmylife.com/view/amour/"+(pagesNb-1)+"?key=4fd9d0cb0463c&language=fr";
            break;
        case 2:     //Animaux
            link = "http://api.fmylife.com/view/animaux/"+(pagesNb-1)+"?key=4fd9d0cb0463c&language=fr";
            break;
        case 3:     //Argent
            link = "http://api.fmylife.com/view/argent/"+(pagesNb-1)+"?key=4fd9d0cb0463c&language=fr";
            break;
        case 4:     //Enfant
            link = "http://api.fmylife.com/view/enfant/"+(pagesNb-1)+"?key=4fd9d0cb0463c&language=fr";
            break;
        case 5:     //Travail
            link = "http://api.fmylife.com/view/travail/"+(pagesNb-1)+"?key=4fd9d0cb0463c&language=fr";
            break;
        case 6:     //Sante
            link = "http://api.fmylife.com/view/sante/"+(pagesNb-1)+"?key=4fd9d0cb0463c&language=fr";
            break;
        case 7:     //Sexe
            link = "http://api.fmylife.com/view/sexe/"+(pagesNb-1)+"?key=4fd9d0cb0463c&language=fr";
            break;
        case 8:     //Inclassable
            link = "http://api.fmylife.com/view/inclassable/"+(pagesNb-1)+"?key=4fd9d0cb0463c&language=fr";
            break;
        case 9:                         //Nouv
            link = "http://api.fmylife.com/view/last/"+(pagesNb-1)+"?key=4fd9d0cb0463c&language=fr";
            break;
        case 10:            //Flop
            link = "http://api.fmylife.com/view/flop/"+(pagesNb-1)+"?key=4fd9d0cb0463c&language=fr";
            break;
        case 11:
            link = "http://api.fmylife.com/view/top/"+(pagesNb-1)+"?key=4fd9d0cb0463c&language=fr";
            break;

        }
    }
    else
    {
        link = lien;
    }



    doc.onreadystatechange = function()
            {
                if (doc.readyState === XMLHttpRequest.DONE)
                {
                    if(value === 0)
                    {
                        console.debug("If : 0")
                        xmlData = doc.responseText
                        obj.replace(Qt.resolvedUrl("MainPage.qml"));
                    }
                    else if(value === 1){                   //Chargement USER
                        console.debug("[Variable] Load Page If : 1")
                        xmlDataUser = doc.responseText
                        obj.push(Qt.resolvedUrl("User.qml"));
                    }
                    else if(value === 2){
                        console.debug("If : 2")
                        xmlDataComment = doc.responseText
                        obj.push(Qt.resolvedUrl("Comments.qml"));
                    }
                    else if(value === 3){       //Validation VDM

                    }

                }
            }
    console.debug("[Script] Openning : "+ link)
    doc.open("GET", link);
    doc.send();
}
